package view.servlet.user;

import pojo.User;
import pojo.vo.Message;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/changePassword")
public class ChangePasswordServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("html/text");
        String origin = req.getParameter("origin");
        String nowPassword = req.getParameter("nowPassword");
        User user = (User) req.getSession().getAttribute("user");
        Long login_date = (long) req.getSession().getAttribute("date");
        Long now_date = System.currentTimeMillis();
        if (now_date-login_date<=60000){
            Message message = userService.changePassword(user.getUsername(), origin, nowPassword);
            resp.getWriter().print(message.getMsg());
            req.getSession().setAttribute("user",user);
        } else {
            resp.getWriter().print("登录超时，修改密码失败，请重新登录！");
            req.getSession().removeAttribute("user");
        }
    }  

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
